package com.ruoyi.lm.controller;

import java.util.List;

import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.lm.domain.LmMessage;
import com.ruoyi.lm.domain.LmSerinfocase;
import com.ruoyi.lm.service.ILmMessageService;
import com.ruoyi.lm.service.ILmSerinfocaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lm.domain.LmSerinfo;
import com.ruoyi.lm.service.ILmSerinfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 服务订单Controller
 * 
 * @author ruoyi
 * @date 2020-09-02
 */
@Controller
@RequestMapping("/lm/serinfo")
public class LmSerinfoController extends BaseController
{
    private String prefix = "lm/serinfo";

    @Autowired
    private ILmSerinfoService lmSerinfoService;
    @Autowired
    private ILmSerinfocaseService lmSerinfocaseService;
    @Autowired
    private ILmMessageService lmMessageService;

    @RequiresPermissions("lm:serinfo:view")
    @GetMapping()
    public String serinfo()
    {
        return prefix + "/serinfo";
    }

    /**
     * 查询服务订单列表
     */
    @RequiresPermissions("lm:serinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LmSerinfo lmSerinfo)
    {
        startPage();
        List<LmSerinfo> list = lmSerinfoService.selectLmSerinfoList(lmSerinfo);
        return getDataTable(list);
    }
    /**
     * 查询服务订单列表
     */
    @RequiresPermissions("lm:serinfo:list")
    @PostMapping("/listChild")
    @ResponseBody
    public TableDataInfo listChild(String mid)
    {
        LmSerinfocase lmSerinfocase = new LmSerinfocase();
        lmSerinfocase.setServiceId(mid);
        List<LmSerinfocase> childList = lmSerinfocaseService.selectLmSerinfocaseList(lmSerinfocase);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(childList);
        return  rspData;
    }

    /**
     * 导出服务订单列表
     */
    @RequiresPermissions("lm:serinfo:export")
    @Log(title = "服务订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LmSerinfo lmSerinfo)
    {
        List<LmSerinfo> list = lmSerinfoService.selectLmSerinfoList(lmSerinfo);
        ExcelUtil<LmSerinfo> util = new ExcelUtil<LmSerinfo>(LmSerinfo.class);
        return util.exportExcel(list, "serinfo");
    }

    /**
     * 新增服务订单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/subdata";
    }

    /**
     * 新增保存服务订单
     */
    @RequiresPermissions("lm:serinfo:add")
    @Log(title = "服务订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LmSerinfo lmSerinfo)
    {
        lmSerinfo.setId(IdWorker.getId());
        return toAjax(lmSerinfoService.insertLmSerinfo(lmSerinfo));
    }

    /**
     * 修改服务订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        LmSerinfo lmSerinfo = lmSerinfoService.selectLmSerinfoById(id);
        mmap.put("lmSerinfo", lmSerinfo);
        LmSerinfocase lmSerinfocase = new LmSerinfocase();
        lmSerinfocase.setServiceId(id);
        mmap.put("serinfoCase",lmSerinfocaseService.selectLmSerinfocaseList(lmSerinfocase));
        return prefix + "/edit";
    }

    /**
     * 查看需求
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") String id, ModelMap mmap)
    {
        LmSerinfo lmSerinfo = lmSerinfoService.selectLmSerinfoById(id);
        mmap.put("lmSerinfo", lmSerinfo);
        return prefix + "/view";
    }

    /**
     * 下架服务
     */
    @GetMapping("/stop/{id}")
    public String stop(@PathVariable("id") String id, ModelMap mmap)
    {
        LmSerinfo lmSerinfo = lmSerinfoService.selectLmSerinfoById(id);
        mmap.put("lmSerinfo", lmSerinfo);
        return prefix + "/stop";
    }

    /**
     * 查看服务案例
     */
    @GetMapping("/editCase/{id}")
    public String editCase(@PathVariable("id") String id, ModelMap mmap)
    {
        LmSerinfocase lmSerinfocase = new LmSerinfocase();
        lmSerinfocase.setServiceId(id);
        mmap.put("serinfoCase",lmSerinfocaseService.selectLmSerinfocaseList(lmSerinfocase));
        return prefix + "lm/serinfocase/edit";
    }

    /**
     * 修改保存服务订单
     */
    @RequiresPermissions("lm:serinfo:edit")
    @Log(title = "服务订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LmSerinfo lmSerinfo)
    {
        return toAjax(lmSerinfoService.updateLmSerinfo(lmSerinfo));
    }/**

     * 下架服务订单
     */
    @RequiresPermissions("lm:serinfo:edit")
    @Log(title = "服务订单", businessType = BusinessType.UPDATE)
    @PostMapping("/stopService")
    @ResponseBody
    public AjaxResult stopService(LmSerinfo lmSerinfo)
    {
        lmSerinfo.setServiceStatus("下架");
        int num = lmSerinfoService.updateLmSerinfo(lmSerinfo);
        if(num >0){
            //下架通知
            //添加拒绝消息提醒
            LmMessage lmMessage =new LmMessage();
            lmMessage.setMessageUser(lmSerinfo.getUserId());
            lmMessage.setMessageTitle("服务下架通知");
            lmMessage.setMessageContent(lmSerinfo.getServiceReason());
            lmMessage.setMessageType("个人");
            lmMessageService.insertLmMessage(lmMessage);
        }
        return toAjax(num);
    }

    /**
     * 删除服务订单
     */
    @RequiresPermissions("lm:serinfo:remove")
    @Log(title = "服务订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lmSerinfoService.deleteLmSerinfoByIds(ids));
    }
}
